import itertools
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def largest_prime_from_digits(number):
    digits = [int(d) for d in str(number)]
    digits.sort(reverse=True)
    for i in range(len(digits), 0, -1):
        for subset in itertools.permutations(digits, i):
            num = int(''.join(map(str, subset)))
            if is_prime(num):
                return num
    return 0

input_number = int(input("Введите четырёхзначное натуральное число: "))
if 1000 <= input_number <= 9999:
    result = largest_prime_from_digits(input_number)
    print(result if result != 0 else "0")
else:
    print("0")